package com.cs.bd.ad.self.request;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.cs.bd.ad.AdSdkResultHelper;
import com.cs.bd.ad.bean.AdModuleInfoBean;
import com.cs.bd.ad.manager.AdSdkManager;
import com.cs.bd.ad.params.AdSdkParamsBuilder;
import com.cs.bd.mopub.MopobViewGroupWarp;
import com.cs.bd.utils.LogUtils;
import com.miui.zeus.mimo.sdk.ad.AdWorkerFactory;
import com.miui.zeus.mimo.sdk.ad.IAdWorker;
import com.miui.zeus.mimo.sdk.listener.MimoAdListener;
import com.mopub.nativeads.NativeAd;
import com.xiaomi.ad.common.pojo.AdType;

/**
 * Created by Administrator on 2018/1/2.
 */

public class XmAdsRequest extends BaseAdsRequest implements MimoAdListener {

    private String mAdsPosition;

    private FrameLayout mParent;

    private IAdWorker myAdWorker;

    private NativeAd mNativeAd;

    public XmAdsRequest(Context mContext, AdSdkParamsBuilder adSdkParams, String mAdsPosition) {
        super(mContext, adSdkParams.mLoadAdvertDataListener);
        this.mAdsPosition = mAdsPosition;
        this.mParent = new MopobViewGroupWarp(mContext);
        mNativeAd = new NativeAd(mParent, adSdkParams);
    }

    @Override
    public void requestAds() {
        try {
            myAdWorker = AdWorkerFactory.getAdWorker(mContext, mParent, this, AdType.AD_STANDARD_NEWSFEED);
            mNativeAd.setIAdWorker(myAdWorker);
            myAdWorker.loadAndShow(mAdsPosition);
        } catch (Exception e) {
            onAdFailed(e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public int getAdsType() {
        return ADS_TYPE_XM;
    }

    @Override
    public void onAdPresent() {
        mNativeAd.onImpression();
        if (null != mLoadAdvertDataListener) {
            mLoadAdvertDataListener.onAdShowed(mNativeAd);
        }
    }

    @Override
    public void onAdClick() {
        mNativeAd.onClick();
        if (null != mLoadAdvertDataListener) {
            mLoadAdvertDataListener.onAdClicked(mNativeAd);
        }
    }

    @Override
    public void onAdDismissed() {
        if (null != mLoadAdvertDataListener) {
            mLoadAdvertDataListener.onAdClosed(mNativeAd);
        }
    }

    @Override
    public void onAdFailed(String s) {
        LogUtils.d("onAdFailed : " + s);
        if (null != mOnRequestListener) {
            mOnRequestListener.onError(s);
        }
//        ImageView imageView = new ImageView(mContext);
//        imageView.setImageResource(android.support.v7.appcompat.R.drawable.abc_btn_check_to_on_mtrl_000);
//        mParent.addView(imageView);
//        onAdLoaded();

    }

    @Override
    public void onAdLoaded() {
        AdModuleInfoBean adModuleInfoBean = AdSdkResultHelper.createNativeAds("xm", mNativeAd);
        if (null != mLoadAdvertDataListener) {
            mLoadAdvertDataListener.onAdInfoFinish(true, adModuleInfoBean);
        }
        if (null != mOnRequestListener) {
            mOnRequestListener.onFinish();
        }
    }
}
